package com.haoqizhe.kernel.mybatis.core.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haoqizhe.kernel.core.entity.PageView;
import com.haoqizhe.kernel.mybatis.utils.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author haoqizhe.li
 * @date 2020-12-29 20:26
 **/
public class BaseController {

    private final String PAGE = "_page";
    private final String SIZE = "_size";
    private final String ORDER_BY = "_order_by";
    private final String DESC = "desc";

    private final int DEFAULT_PAGE = 1;
    private final int DEFAULT_SIZE = 10;

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    protected int getPageNum() {
        String page = request.getParameter(PAGE);
        int current = DEFAULT_PAGE;
        if (StrUtil.isNotBlank(page)) {
            current = NumberUtil.isNumber(page) ? Integer.valueOf(page) : current;
        }
        return current;
    }

    protected int getSizeNum() {
        String size = request.getParameter(SIZE);
        int pageSize = DEFAULT_SIZE;
        if (StrUtil.isNotBlank(size)) {
            pageSize = NumberUtil.isNumber(size) ? Integer.valueOf(size) : pageSize;
        }
        return pageSize;
    }

    protected String getOrderBy() {
        return request.getParameter(ORDER_BY);
    }

    protected <T> Page<T> getPage() {

        int current = this.getPageNum();
        int pageSize = this.getSizeNum();

        Page<T> p = new Page<T>(current, pageSize);
        p.setCurrent(current);
        p.setSize(pageSize);

        return p;
    }

    protected <T> QueryWrapper<T> getWrapper() {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        Map<String, String[]> map = request.getParameterMap();
        if (CollUtil.isNotEmpty(map)) {
            for (String key : map.keySet()) {
                if (ORDER_BY.equals(key)) {
                    ParamUtil.setOrderBy(map.get(key)[0], wrapper);
                    continue;
                }

                if (!key.equals(PAGE) && !key.equals(SIZE)) {
                    setWrapper(key, map.get(key)[0], wrapper);
                }
            }
        }
        return wrapper;
    }

    protected <T> QueryWrapper<T> getWrapperByMap(Map<String, String> map, List<String> ignoreKeyList) {

        if (CollUtil.isEmpty(ignoreKeyList)) {
            ignoreKeyList = new ArrayList<>();
        }

        ignoreKeyList.add(PAGE);
        ignoreKeyList.add(SIZE);

        return ParamUtil.getWrapperByMap(map, ignoreKeyList);
    }

    /**
     * 封装map 查询参数
     *
     * @return
     * @throws
     * @author haoqizhe.li
     * @date 2018/4/8 13:51
     **/
    protected <T> void setWrapper(String key, String value, QueryWrapper<T> wrapper) {
        ParamUtil.setWrapper(key, value, wrapper);
    }

    protected <T> PageView<T> pageBuild(Page<T> page) {
        return ParamUtil.pageBuild(page);
    }

    protected Map<String, String> getParameterMap() {
        Map<String, String[]> pm = request.getParameterMap();
        Map<String, String> map = new HashMap<>(0);
        if (CollUtil.isNotEmpty(pm)) {
            for (String key : pm.keySet()) {
                if (!key.equals(PAGE) &&
                        !key.equals(SIZE)) {
                    map.put(key, pm.get(key)[0]);
                }
            }
        }
        return map;
    }
}
